package solver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Util {

  /**
   * Method used to invert a java MAP
   * 
   * @return
   */
  public static <V, K> Map<V, K> invert(Map<K, V> map) {

    Map<V, K> inv = new HashMap<V, K>();

    for (Entry<K, V> entry : map.entrySet())
      inv.put(entry.getValue(), entry.getKey());

    return inv;
  }

  /**
   * Method that receives a clause on the DIMARCS format as a String, and
   * returns the same clause on the array format.
   * 
   * @param s
   * @return
   */

  public static int[] dimacsClauseToIntArray(String s) {
    ArrayList<Integer> output = new ArrayList<Integer>();
    String number = "";
    for (int i = 0; i < s.length() - 1; i++) {
      char c = s.charAt(i);
      if (c != ' ') {
        number += c;
      } else if (c == 0 && number.length() == 0) {
        break;
      } else {
        output.add(Integer.parseInt(number));
        number = "";
      }
    }
    return convertIntegers(output);
  }

  /**
   * Convert an arraylist of ints in an array of ints
   * 
   * @param integers
   * @return
   */
  public static int[] convertIntegers(ArrayList<Integer> integers) {
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++) {
      ret[i] = iterator.next().intValue();
    }
    return ret;
  }

  /**
   * Reads a file containing various valid configurations. One configuration in
   * each line. Returns an arraylist of these configurations.
   * 
   * @param filePath
   * @return
   */

  public static ArrayList<String[]> readFileToTest(String filePath) {
    try {
      ArrayList<String[]> listOfListOfFeatures = new ArrayList<String[]>();
      // Open the file that is the first
      // command line parameter
      FileInputStream fstream = new FileInputStream(filePath);
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      // Read File Line By Line
      while ((strLine = br.readLine()) != null) {
        String[] configuration = strLine.split(" ");
        listOfListOfFeatures.add(configuration);
      }
      // Close the input stream
      in.close();
      return listOfListOfFeatures;
    } catch (Exception e) {// Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
    return null;
  }

  /**
   * Method used to create a file which contains valid configurations
   * 
   * @param features
   * @param filename
   */

  public static void createFeaturesFile(ArrayList<String> features,
      String filename) {
    String fileName = "../SEF/featureFiles"
        + System.getProperty("file.separator") + filename;
    try {
      FileWriter file = new FileWriter(fileName);
      BufferedWriter writer = new BufferedWriter(file);
      for (String feature : features) {
        writer.write(feature);
      }
      writer.flush();
      writer.close();
      writer.close();
    } catch (FileNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
  
  public static int[] addingZeroElementArray(int[] input) {
    int[] output = new int[input.length+1];
    output[0] = -1;
    for(int i = 1; i < output.length; i++) {
      output[i] = input[i-1];
    }
    return output;
  }

}
